<?php
/*
Plugin Name: WP FancyBox
Plugin URI: http://code.google.com/p/wp-fancybox/
Description: Overlay images on pages using FancyBox. <em>FancyBox wrote by <a href="http://fancy.klade.lv/">Janis Skarnelis</a></em>.
Author: Dmitry Chestnykh
Version: 1.1
Author URI: http://www.codingrobots.com/
*/
/*

License (MIT-style)

Copyright (C) 2008 Dmitry Chestnykh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


function fancybox_config_page() {
	?>
	<div class="wrap">
	<h2>WP FancyBox</h2>

	<form method="post" action="options.php">
	<?php wp_nonce_field('update-options'); ?>

	<h3><?php _e('Appearance') ?></h3>

	<table class="form-table">
	<tr valign="top">
	<th scope="row"><?php _e('Hide on content click') ?></th>
	<td><input type="checkbox" name="fancybox_hideOnContentClick" <?php if(get_option('fancybox_hideOnContentClick')) echo "checked"; ?> value="1" /></td>
	</tr>

	<tr valign="top">
	<th scope="row"><?php _e('Show overlay') ?></th>
	<td><input type="checkbox" name="fancybox_overlayShow" <?php if(get_option('fancybox_overlayShow')) echo "checked"; ?> value="1" /></td>
	</tr>

	<tr valign="top">
	<th scope="row"><?php _e('Overlay opacity') ?></th>
	<td><input type="text" name="fancybox_overlayOpacity" value="<?php echo get_option('fancybox_overlayOpacity'); ?>" /> 
		&nbsp; <em><?php _e('From 0 to 1') ?></em></td>
	</tr>


	<tr valign="top">
	<th scope="row"><?php _e('Zoom in speed') ?></th>
	<td><input type="text" name="fancybox_zoomSpeedIn" value="<?php echo get_option('fancybox_zoomSpeedIn'); ?>" />	&nbsp; <em><?php _e('milliseconds') ?></td>
	</tr>

	<tr valign="top">
	<th scope="row"><?php _e('Zoom out speed') ?></th>
	<td><input type="text" name="fancybox_zoomSpeedOut" value="<?php echo get_option('fancybox_zoomSpeedOut'); ?>" /> 	&nbsp; <em><?php _e('milliseconds') ?></td>
	</tr>
	</table>

	<h3><?php _e('Advanced') ?></h3>

	<table class="form-table">
	<tr valign="top">
	<th scope="row"><?php _e('jQuery Expression') ?></th>
	<td><input type="text" name="fancybox_expression" value="<?php echo get_option('fancybox_expression'); ?>" /> 	&nbsp; <em><?php _e('Default is ') ?><code style="color: navy">jQuery("img.size-thumbnail").parent()<code></td>
	</tr> 
	</table>

	<input type="hidden" name="action" value="update" />
	<input type="hidden" name="page_options" value="fancybox_hideOnContentClick,fancybox_overlayShow,fancybox_overlayOpacity,fancybox_zoomSpeedIn,fancybox_zoomSpeedOut,fancybox_expression" />

	<p class="submit">
	<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
	</p>

	</form>
	</div>
	<?php
}


function fancybox_add_menu_item() 
{
	add_options_page('WP FancyBox', 'FancyBox', 'manage_options', 'fancybox', 'fancybox_config_page');
}


function fancybox_write_headers() 
{
	/* Stylesheet */
	echo '<link rel="stylesheet" href="'.get_option('home').'/wp-content/plugins/wp-fancybox/fancy.min.css" type="text/css">';
	echo "\n";
	
	/* JS */
	$options = '';
	if (get_option('fancybox_hideOnContentClick'))
		$options .= '"hideOnContentClick":true,';
	if (get_option('fancybox_overlayShow'))
		$options .= '"overlayShow":true,';
	if (get_option('fancybox_overlayOpacity') != '')
		$options .= '"overlayOpacity":'.get_option('fancybox_overlayOpacity').",";
	if (get_option('fancybox_zoomSpeedIn') != '')
		$options .= '"zoomSpeedIn":'.get_option('fancybox_zoomSpeedIn').",";
	if (get_option('fancybox_zoomSpeedOut') != '')
		$options .= '"zoomSpeedOut":'.get_option('fancybox_zoomSpeedOut').",";

	$expr = get_option('fancybox_expression');
	if (empty($expr))
		$expr = 'jQuery("img.size-thumbnail").parent()'; // set default
	
	echo '<script type="text/javascript">jQuery(document).ready(function(){'.$expr.'.fancybox({'.$options.'});});</script>';
	echo "\n";
}


wp_enqueue_script('fancybox', '/wp-content/plugins/wp-fancybox/jquery.fancybox.min.js', array('jquery'), '1.1');

add_action('wp_head', 'fancybox_write_headers');
add_action('admin_menu', 'fancybox_add_menu_item');

?>